package com.eportal.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.eportal.api.MdOpenAddAgentJson.Agent;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MdOpenQryKeysJson extends MdOpenBaseJson{
	
	static Logger log = Logger.getLogger(MdOpenQryKeysJson.class);
	public static MdOpenQryKeysJson parse(String json){
		MdOpenQryKeysJson bean  = new MdOpenQryKeysJson();
		Key key = bean.new Key();
		MdOpenBaseJson.parse(json,bean);
		bean.setData(key);
		if(bean.isSuccess()){
			//如果请求是成功的,解析msg部分,
			JSONObject obj = JSONObject.fromObject(json);
			JSONArray arr = obj.getJSONArray("msg");
			JSONObject keyJson =  arr.getJSONObject(0);
			bean.getData().setAlgorithmVer(keyJson.getString("algorithmVer"));
			bean.getData().setLock_id(keyJson.getString("lock_id"));
			bean.getData().setLock_name(keyJson.getString("lock_name"));
			bean.getData().setAlgorithmVer(keyJson.getString("algorithmVer"));;
			bean.getData().setCommunity(keyJson.getString("community"));
//			key.setAlgorithmVer(keyJson.getString("algorithmVer"));
//			key.setLock_id(keyJson.getString("lock_id"));
//			key.setLock_name(keyJson.getString("lock_name"));
			bean.setMsg(key);
		}
		
		return bean;
	}
	
	public static Map<String, Object> parseList(String json){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			List<Key> list = new ArrayList<Key>();
			MdOpenQryKeysJson bean  = new MdOpenQryKeysJson();
			MdOpenBaseJson.parse(json,bean);
			result.put("code", bean.getCode());
			result.put("msg", bean.getMsg());
			if(bean.isSuccess()){
				//如果请求是成功的,解析msg部分,
				JSONObject obj = JSONObject.fromObject(json);
				JSONArray arr = obj.getJSONArray("msg");
				Key key;
				JSONObject keyJson;
				for (int i = 0; i < arr.size(); i++) {
					 key = bean.new Key();
					 keyJson =  arr.getJSONObject(i);
					 key.setAlgorithmVer(keyJson.getString("algorithmVer"));
					 key.setLock_id(keyJson.getString("lock_id"));
					 key.setLock_name(keyJson.getString("lock_name"));
					 key.setAlgorithmVer(keyJson.getString("algorithmVer"));;
					 key.setCommunity(keyJson.getString("community"));
					 key.setPid(keyJson.getString("pid"));
					 key.setKsid(keyJson.getString("ksid"));
					 list.add(key);
				}
				result.put("list", list);
			}
		} catch (Exception e) {
			result.put("code", 0);
  	        result.put("msg", "申请钥匙解析失败");
  	      log.error("error", e);
		}
		return result;
	}
	
	private Key data;
	
	/**
	 * data  
	 * @return the data
	 */
	public Key getData() {
		return data;
	}
	

	/**
	 * data 
	 * @param data
	 */
	public void setData(Key data) {
		this.data = data;
	}
	

	public class Key{
		 Key(){
			 
		 }
		private String agtcode;
		private String app_key;
		private String algorithmVer;
		private String user_id;
		private String lock_name;
		
		private String community;
		private String lock_id;
		private String pid;
		private String ksid;
		
		public String getKsid() {
			return ksid;
		}
		
		public void setKsid(String ksid) {
			this.ksid = ksid;
		}
		

		public String getAlgorithmVer() {
			return algorithmVer;
		}

		public void setAlgorithmVer(String algorithmVer) {
			this.algorithmVer = algorithmVer;
		}

		public String getLock_name() {
			return lock_name;
		}

		public void setLock_name(String lock_name) {
			this.lock_name = lock_name;
		}

		public String getLock_id() {
			return lock_id;
		}

		public void setLock_id(String lock_id) {
			this.lock_id = lock_id;
		}

		/**
		 * agtcode  
		 * @return the agtcode
		 */
		public String getAgtcode() {
			return agtcode;
		}
		

		/**
		 * agtcode 
		 * @param agtcode
		 */
		public void setAgtcode(String agtcode) {
			this.agtcode = agtcode;
		}
		

		/**
		 * app_key  
		 * @return the app_key
		 */
		public String getApp_key() {
			return app_key;
		}
		

		/**
		 * app_key 
		 * @param app_key
		 */
		public void setApp_key(String app_key) {
			this.app_key = app_key;
		}
		

		/**
		 * user_id  
		 * @return the user_id
		 */
		public String getUser_id() {
			return user_id;
		}
		

		/**
		 * user_id 
		 * @param user_id
		 */
		public void setUser_id(String user_id) {
			this.user_id = user_id;
		}
		

		/**
		 * community  
		 * @return the community
		 */
		public String getCommunity() {
			return community;
		}
		

		/**
		 * community 
		 * @param community
		 */
		public void setCommunity(String community) {
			this.community = community;
		}
		

		/**
		 * pid  
		 * @return the pid
		 */
		public String getPid() {
			return pid;
		}
		

		/**
		 * pid 
		 * @param pid
		 */
		public void setPid(String pid) {
			this.pid = pid;
		}
		
		
	}
}
